package com.yk.faceapplication.activity;

import java.util.Locale;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.allinpay.xed.R;
import com.yk.faceapplication.MyApplication;
import com.yk.faceapplication.util.LogDebugger;

@SuppressLint("HandlerLeak")
public class BaseActivity extends Activity implements View.OnClickListener {
    private final String TAG = "loan_";
    private int mLayoutResId;
    protected String activityStatus;
    protected boolean isLive = false;
    private String mClassName;
    protected static SharedPreferences preferences;
    protected static SharedPreferences.Editor editor;
    protected MyApplication mApplication;
    protected String mToken;

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back_iv) {
            finishAct();
        }
    }

    protected void setTitle(String title) {
        if (findViewById(R.id.title_tv) != null) {
            ((TextView) findViewById(R.id.title_tv)).setText(title);
        }
    }

    protected void setTitleById(int id) {
        if (findViewById(R.id.title_tv) != null) {
            ((TextView) findViewById(R.id.title_tv)).setText(getResources().getString(id));
        }
    }

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        LogDebugger.info(getClass().getSimpleName() + "  onCreate");
        mClassName = getClass().getSimpleName();
        int end = mClassName.length() - 8;
        mLayoutResId = getResources().getIdentifier(TAG + mClassName.toLowerCase(Locale.ENGLISH).substring(0, end), "layout", getPackageName());
        try {
            if (mLayoutResId != 0) setContentView(mLayoutResId);
            preferences = getSharedPreferences("mydata", MODE_PRIVATE);
            editor = preferences.edit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (findViewById(R.id.back_iv) != null) {
            findViewById(R.id.back_iv).setOnClickListener(this);
        }
        initTransitionAnim();// 加载动画
        mApplication = ((MyApplication) this.getApplicationContext());
        if(mApplication.getToken() != null){
            mToken = mApplication.getToken();
        }
    }

    /**
     * 页面切换动画
     */
    protected void initTransitionAnim() {
        overridePendingTransition(R.anim.in_from_right, R.anim.out_from_left);
    }

    /**
     * 销毁当前ACTIVITY,并添加切换动画
     */
    protected void finishAct() {
        finish();
        overridePendingTransition(R.anim.in_from_left, R.anim.out_from_right);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        isLive = true;
        super.onResume();
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        isLive = false;
        super.onStop();
    }

    protected Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            onHandleMessage(msg);
        }
    };

    protected final void sendMessage(int what) {
        mHandler.sendMessage(mHandler.obtainMessage(what));
    }

    protected final void sendMessage(int what, int arg1, int arg2) {
        mHandler.sendMessage(mHandler.obtainMessage(what, arg1, arg2));
    }

    protected final void sendMessageDelayed(int what, long delayMillis) {
        mHandler.sendMessageDelayed(mHandler.obtainMessage(what), delayMillis);
    }

    protected final void sendMessageDelayed(int what, int arg1, int arg2, long delayMillis) {
        mHandler.sendMessageDelayed(mHandler.obtainMessage(what, arg1, arg2), delayMillis);
    }

    protected final void sendMessage(int what, Object obj) {
        mHandler.sendMessage(mHandler.obtainMessage(what, obj));
    }

    protected final void removeMessages(int what) {
        mHandler.removeMessages(what);
    }

    protected void onHandleMessage(Message msg) {
        switch (msg.what) {
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finishAct();
        }
        return super.onKeyDown(keyCode, event);
    }

}
